package cn.itcast.bos.web.action.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{
	
	
	private Standard standard =new Standard();
	@Override
	public Standard getModel() {
		return standard;
	}
	
	private Integer page;
	private Integer rows;
	
	public void setPage(Integer page) {
		this.page = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}
	
	
	@Autowired
	private StandardService standardservice;
	@Action(value="standard_save",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String save(){
		standardservice.save(standard);
		
		return SUCCESS;
	}

	
	@Action(value="standard_pageQuey",results={@Result(name="success",type="json")})
	public String pagequry(){
//		注意此处page要减一  因为页面是从1开始,不过查询是从0开始
		Pageable pageable=new PageRequest(page-1, rows);
		Page<Standard> pagedata=standardservice.findPageData(pageable);
		
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("total", pagedata.getTotalElements());
		result.put("rows", pagedata.getContent());
		
		ServletActionContext.getContext().getValueStack().push(result);
		
		
		return SUCCESS;
	}
	
	
	
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String standardfindall(){
		
		List<Standard> list=standardservice.findall();
		ServletActionContext.getContext().getValueStack().push(list);
		
		return SUCCESS;
	}
	
	
	
}
